﻿using ArticleandSiteData;
using ModelClassLibrary;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍

namespace SplitViewOptions
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class EditUserinfoPage : Page
    {
        public EditUserinfoPage()
        {
            this.InitializeComponent();
        }
        private static string applicationid = "1f570053c58338812a8970abbbaaa623";
        private static string restkeyid = "d803ea3828a42eba94ecbc9ea47d435a";
        private async void registerbrn_Click(object sender, RoutedEventArgs e)
        {
            if (App.Loginuser == null) {

                await App.showmessage("请先登录");
                return;


            }
            if (registuserpassword1.Text != registeuerpassword.Text) return;
            registerbrn.IsEnabled = false;
            //string jsonstr = "{" + "\"" + "username" + "\"" + ":" + "\"" + registeruername.Text + "\"" + "," + "\"" + "password" + "\"" + ":" + "\"" + registeuerpassword.Text + "\"" + "," + "\"" + "phone" + "\"" + ":" + "\"" + phonebox.Text + "\"" + "email" + "\"" + ":" + "\"" + emailbox.Text + "\"" +
            //     "mainwebspace" + "\"" + ":" + "\"" + mainwebspacebox.Text + "\"" + "isvisible" + "\"" + ":" + "\"" + phonebox.Text + "\"" +
            //    "}";
            Dictionary<string, string> userdic = new Dictionary<string, string>();
            //userdic.Add("username", registeruername.Text);
            userdic.Add("password", registeuerpassword.Text);

            userdic.Add("phone", phonebox.Text);

            userdic.Add("email", emailbox.Text);

            userdic.Add("mainwebspace", mainwebspacebox.Text);
            userdic.Add("likesbox", likesbox.Text);
            userdic.Add("introducebox", introducebox.Text);
            if (visiblesw.IsOn)
            {
                userdic.Add("isvisible", "1");
            }
            else
            {
                userdic.Add("isvisible", "0");
            }
            string jsonstrdic = RequestHelper.DictionaryTojsonstr(userdic);
            StringContent content = new StringContent(jsonstrdic);
            string m = "https://api.bmob.cn/1/users/"+App.Loginuser.objectId;
            var responce = await modeify<ALLSitesResult>(m, jsonstrdic);
            if (responce.StatusCode == System.Net.HttpStatusCode.OK)
            {
                await App.showmessage("修改成功");
                //  MessageDialog one = new MessageDialog("注册成功,请登录");
                //await  one.ShowAsync();
                // registerbrn.IsEnabled = true;
            }
            else
            {
                await App.showmessage("修改失败");
                // MessageDialog one = new MessageDialog("注册失败");
                //await one.ShowAsync();
                registerbrn.IsEnabled = true;
            }
        }
        public async  Task<System.Net.Http.HttpResponseMessage> modeify<HttpResponseMessage>(string str, string jsonstr)
        {
            System.Net.Http.HttpResponseMessage resualist;
            resualist = await PutHttpstringrequest(str, jsonstr);
            //  resualist = (T)JsonConvert.DeserializeObject(resultstr, typeof(T));
            return resualist;
        }
        public  async Task<HttpResponseMessage> PutHttpstringrequest(string requesturl, string jsonstr)
        {
            var myClient = new HttpClient();
            var myRequest = new HttpRequestMessage(HttpMethod.Put, requesturl);
            // HttpContent content = new StringContent(@"{ ""url"": ""http://cili006.com/""}");
            HttpContent content = new StringContent(jsonstr, UTF8Encoding.UTF8, "text/plain");
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            // Send a request asynchronously continue when complete 
            myRequest.Content = content;
            //myRequest.Version = Version.Parse("1.1");
            myRequest.Headers.Add("X-Bmob-Application-Id", applicationid);
            myRequest.Headers.Add("X-Bmob-REST-API-Key", restkeyid);
            myRequest.Headers.Add("X-Bmob-Session-Token", App.Loginuser.sessionToken);
            //HttpContent content= 
            //myRequest.Content=
            //// This property represents the client preference for the HTTP protocol version.
            //// The default value for UWP apps is 2.0.
            //Debug.WriteLine(myRequest.Version.ToString());
            var response = await myClient.SendAsync(myRequest);
            return response;
            //string streamcontent = await response.Content.ReadAsStringAsync();
            //return streamcontent;
        }

    }
}
